/*
 * CLASSE que irá implementar a interface ENDERECODAO
 */

package br.com.jkar.dao;

import br.com.jkar.bean.Endereco;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;

/**
 * 
 * @author Kamila Berto <kah.berto@gmail.com>
 */
public class EnderecoDAOImpl implements EnderecoDAO{
    
    Connection conn = null;
    PreparedStatement psmt = null;   
    ResultSet rs;

    @Override
    public Endereco salvar(Endereco endereco) throws Exception {
        try {
            conn = FabricarConexao.abrirConexao();
            String sql = "INSERT INTO endereco ((rua, numero, compl, bairro, cidade, "
                    + "estado, cep) VALUES (?, ?, ?, ?, ?, ?, ?)";
            psmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            psmt.setString(1, endereco.getRua());
            psmt.setString(2, endereco.getNumero());
            psmt.setString(3, endereco.getCompl());
            psmt.setString(4, endereco.getBairro());
            psmt.setString(5, endereco.getCidade());
            psmt.setString(6, endereco.getEstado());
            psmt.setString(7, endereco.getCep());
            psmt.executeUpdate();
            rs = psmt.getGeneratedKeys();
            rs.next();
            endereco.setId(rs.getInt(1));
            return endereco;
        }catch (Exception e) {
            System.out.println("Erro ao gravar dados de endereço no BD: " + e.getMessage());
        }finally{
            FabricarConexao.fecharConexao(conn, psmt, rs);
        }
        return null;
    }

    @Override
    public Endereco alterar(Endereco endereco) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void excluir(Integer id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Endereco> listar() throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Endereco procurar(Integer id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
